package com.edu.exam.entity.examination.paper;

import com.edu.exam.enums.examination.ArrangementTaskType;
import com.edu.exam.enums.system.TaskPriority;
import com.edu.exam.enums.examination.paper.PaperProcessingTaskStatus;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 自动安排任务实体类
 *
 * <p>自动安排任务实体，存储自动安排任务的完整信息和执行状态。
 * 每个任务对应一次批量或单个考试的自动安排，包含任务配置、
 * 执行状态、统计信息等完整内容。</p>
 *
 * @author 系统生成
 * @version 2.0
 * @since 2025-11-09
 */
@Entity
@Table(name = "auto_arrangement_task")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AutoArrangementTask {

    /**
     * 任务ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long taskId;

    /**
     * 任务名称
     */
    @Column(name = "task_name", length = 200, nullable = false)
    private String taskName;

    /**
     * 任务类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "task_type", length = 20, nullable = false)
    private ArrangementTaskType taskType;

    /**
     * 任务描述
     */
    @Column(name = "task_description", columnDefinition = "TEXT")
    private String taskDescription;

    /**
     * 任务优先级
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "task_priority", length = 20, nullable = false)
    private TaskPriority taskPriority;

    /**
     * 考试ID列表（JSON格式）
     */
    @Column(name = "exam_ids", columnDefinition = "TEXT", nullable = false)
    private String examIds;

    /**
     * 安排配置（JSON格式）
     */
    @Column(name = "arrangement_config", columnDefinition = "TEXT", nullable = false)
    private String arrangementConfig;

    /**
     * 优化配置（JSON格式）
     */
    @Column(name = "optimization_config", columnDefinition = "TEXT")
    private String optimizationConfig;

    /**
     * 任务状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "task_status", length = 20, nullable = false)
    private PaperProcessingTaskStatus taskStatus;

    /**
     * 进度百分比
     */
    @Column(name = "progress_percentage")
    private Integer progressPercentage;

    /**
     * 当前执行步骤
     */
    @Column(name = "current_step", length = 100)
    private String currentStep;

    /**
     * 错误信息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 总考试数量
     */
    @Column(name = "total_exams")
    private Integer totalExams;

    /**
     * 成功安排数量
     */
    @Column(name = "successful_arrangements")
    private Integer successfulArrangements;

    /**
     * 失败安排数量
     */
    @Column(name = "failed_arrangements")
    private Integer failedArrangements;

    /**
     * 处理时间（秒）
     */
    @Column(name = "processing_time")
    private Integer processingTime;

    /**
     * 优化时间（毫秒）
     */
    @Column(name = "optimization_time")
    private Integer optimizationTime;

    /**
     * 执行节点
     */
    @Column(name = "execution_node", length = 100)
    private String executionNode;

    /**
     * 执行线程ID
     */
    @Column(name = "execution_thread_id", length = 100)
    private String executionThreadId;

    /**
     * 内存使用量（字节）
     */
    @Column(name = "memory_usage")
    private Long memoryUsage;

    /**
     * 创建人
     */
    @Column(name = "created_by", length = 50, nullable = false)
    private String createdBy;

    /**
     * 更新人
     */
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 完成时间
     */
    @Column(name = "completed_time")
    private LocalDateTime completedTime;

    /**
     * 任务结果列表
     */
    @OneToMany(mappedBy = "task", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<AutoArrangementResult> results;
}